package com.colin.aiyi.presenter.Imp_de;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.dao.ImpxUtils3_de.ImpOther;
import com.colin.aiyi.dao.ImpxUtils3_de.ImpSay;
import com.colin.aiyi.entity.BannerEntity;
import com.colin.aiyi.entity.DataEntity;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.entity.response.YiJuAndYiShuoResponse;
import com.colin.aiyi.ui.Home.teacherSay.IViewSayTeacher;
import com.colin.aiyi.dao.ImpxUtils3_de.IService;
import com.colin.aiyi.presenter.Interface_Derecated.AbsPresenter;
import com.colin.aiyi.ui.sayDetail.TeacherSayDetailsActivity;
import com.colin.aiyi.utils.MusicUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by hl on 2017/5/8.
 */
@Deprecated
public class ImpSayTeacher extends AbsPresenter<YiJuAndYiShuoResponse> {
    private boolean pullMore=false;
    private int totalCount=0;
    private int pageNum = 1;
    private String contentType="1";//1:人气 2：最新 ,已购买3
    private String lastType="1";  //上次类型
    IViewSayTeacher iViewSayTeacher;
    IService<YiJuAndYiShuoResponse> iService=new ImpSay();
    private int totalpageNum;


    public ImpSayTeacher(IViewSayTeacher iViewSayTeacher) {
        this.iViewSayTeacher = iViewSayTeacher;
    }



    @Override
    public void onSuccess(Object t) {
        if (t instanceof YiJuAndYiShuoResponse)
        {
            dfd((YiJuAndYiShuoResponse) t);
        }
        if (t instanceof BannerEntity)
        {
            iViewSayTeacher.bindBanner((BannerEntity) t);
        }
    }


    private void dfd(YiJuAndYiShuoResponse d){
        DataEntity dataEntity = d.getData();
        totalCount=dataEntity.getCount();

        List<YiJuAndYiShuoEntity> list = dataEntity.getItems();
        if (!lastType.equals(contentType))
        {
            pullMore=false;
        }
        lastType=contentType;
        if (pullMore)
        {
            iViewSayTeacher.pullMoreComlite();
         }else {
            iViewSayTeacher.getData().clear();
            iViewSayTeacher.refreshComplite();
        }
        if (list!=null&list.size()>0)
        {
            iViewSayTeacher.getData().addAll(list);
        }
    }

    /*********
     *
     * @param //pageNum  页面序号
     * @param value   内容，类型，
     */
    @Override
    public void getSayList(String... value) {
        this.contentType=value[value.length-1];
        this. pageNum= Integer.parseInt(value[0]);
        iService.getSayList(this,value);
    }




    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        YiJuAndYiShuoEntity item = (YiJuAndYiShuoEntity) adapter.getData().get(position);
        switch (view.getId()) {
            case  R.id.img_video_play:
            case  R.id.img_video:
                MusicUtil.getInstance().setMusicTitle(item.getTitle());
                MusicUtil.getInstance().setMusicPic(Constant.BASE_URL+item.getPictureUrl());
                MusicUtil.getInstance().setDataSource(iViewSayTeacher.getContext(), item.getVoiceUrl());
                item.setWatchCount(item.getWatchCount()+1);
                item.setWatchCountActive(1);
                ImpOther.getInstance().doWatch(item.getId()+"");
                adapter.notifyItemChanged(position+adapter.getHeaderLayoutCount(),item);
                break;
            case R.id.text_watch:
                break;
            case R.id.text_like:
                int a= ((TextView)view).getCompoundDrawables()[0].getLevel();
                int sum= Integer.parseInt(((TextView) view).getText().toString());
                sum++;
                a++;
                boolean operType=true;
                if (a==2)
                {
                    sum-=2;
                    a-=2;
                    operType=false;
                }
                ((TextView) view).setText(sum+"");
                ((TextView)view).getCompoundDrawables()[0].setLevel(a);
                ImpOther.getInstance().doLike(operType,item.getId()+"","1");
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemClick(adapter, view, position);
        Intent intent = new Intent(iViewSayTeacher.getContext(), TeacherSayDetailsActivity.class);
        String id=((YiJuAndYiShuoEntity) adapter.getData().get(position)).getId()+"";
        intent.putExtra("id",id);
        iViewSayTeacher.startNextAc(intent);
    }

    public void onRefresh() {
        pullMore=false;
        pageNum=1;
        getSayList(pageNum+"",contentType);
    }

    @Override
    public void onLoadMoreRequested() {

        if (iViewSayTeacher.getData().size() < totalCount) {
            pullMore=true;
            pageNum++;
            getSayList(pageNum+"",contentType);
        } else {
            iViewSayTeacher.pullMoreNoMore();

        }
    }




    @Override
    public void getBannerList() {
        super.getBannerList();
        RequestParams params=new RequestParams(Constant.list);
     //   params.addBodyParameter("accessToken", SharePreferenceUtil.getObject(iViewSayTeacher.getContext(), User.class).getAccessToken());
        params.addBodyParameter("accessToken","f8c40469e1ef4d958e738b30eb777585");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                BannerEntity bannerEntity=gson.fromJson(result,BannerEntity.class);
                sendMsgToView(bannerEntity);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
